import 'package:flutter/foundation.dart';
import 'package:loxen/mode/base_mode.dart';
import 'package:loxen/mode/like_mode.dart';
import 'package:loxen/mode/mine/like_list.dart';
import 'package:rxdart/rxdart.dart';
import 'package:loxen/api/api.dart';

mixin LikeMixin {
  Stream<LikeList> userLikes(
      {String userId = '0', String page = '1', String pageSize}) {
    return ApiService.userLikes(userId, page, pageSize)
        .flatMap((value) => compute(decodeLikeList, value.data).asStream())
        .flatMap((value) => Stream.value(value.data));
  }

  Stream<int> likeComment(String id) {
    return ApiService.likeComment(id)
        .flatMap((value) => Stream.value(value.statusCode));
  }

  Stream<int> unLikeComment(String id) {
    return ApiService.unLikeComment(id)
        .flatMap((value) => Stream.value(value.statusCode));
  }

  Stream<Like> like(String type, String id) {
    return ApiService.likeSomething(type, id)
        .flatMap((value) => compute(decodeLikeBean, value.data).asStream())
        .flatMap((value) => Stream.value(value.data));
  }

  Stream<Like> unLike(String type, String id) {
    return ApiService.unLikeSomething(type, id)
        .flatMap((value) => compute(decodeLikeBean, value.data).asStream())
        .flatMap((value) => Stream.value(value.data));
  }

  static BaseMode<LikeList> decodeLikeList(dynamic json) {
    return BaseMode<LikeList>(json);
  }

  static BaseMode<Like> decodeLikeBean(dynamic json) {
    return BaseMode<Like>(json);
  }
}
